package com.seqb.manage.loupan.controller;

import com.github.pagehelper.Page;
import com.seqb.annotation.Log;
import com.seqb.common.Query;
import com.seqb.common.ResultVo;
import com.seqb.common.controller.BaseController;
import com.seqb.manage.loupan.domain.LoupanDO;
import com.seqb.manage.loupan.service.LoupanService;
import com.seqb.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author lilinshen
 * @title 楼盘信息表
 * @description 请填写相关描述
 * @date 2020-01-18
 */
@Controller
@RequestMapping("/manage/loupan/loupan")
public class LoupanController extends BaseController {
    @Autowired
    private LoupanService loupanService;

    @GetMapping()
    @Log("楼盘信息表列表")
    String Loupan() {
        return "manage/loupan/loupan.list";
    }

    @ResponseBody
    @GetMapping("/list")
    @Log("楼盘信息表列表")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<LoupanDO> page = loupanService.list(query);
        PageUtils pageUtils = new PageUtils(page.getResult(), page.getTotal());
        return pageUtils;
    }

    @GetMapping("/add")
    @Log("楼盘信息表添加")
    String add() {
        return "manage/loupan/loupan.add";
    }

    @GetMapping("/edit/{id}")
    @Log("楼盘信息表编辑")
    String edit(@PathVariable("id") Long id, Model model) {
        LoupanDO loupan = loupanService.get(id);
        model.addAttribute("loupan", loupan);
        return "manage/loupan/loupan.edit";
    }

    /**
     * 添加保存
     */
    @ResponseBody
    @PostMapping("/save")
    @Log("楼盘信息表添加保存")
    public ResultVo save(LoupanDO loupan) {
        if (loupanService.save(loupan) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 编辑保存
     */
    @ResponseBody
    @RequestMapping("/update")
    @Log("楼盘信息表编辑保存")
    public ResultVo update(LoupanDO loupan) {
        loupanService.update(loupan);
        return ResultVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @Log("楼盘信息表删除")
    public ResultVo remove(Long id) {
        if (loupanService.remove(id) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @Log("楼盘信息表批量删除")
    public ResultVo remove(@RequestParam("ids[]") Long[] ids) {
        loupanService.batchRemove(ids);
        return ResultVo.ok();
    }

    /**
     * 楼盘信息表爬取
     */
    @PostMapping("/reptile")
    @ResponseBody
    @Log("楼盘信息表爬取")
    public ResultVo reptile() {
        return loupanService.reptile();
    }

    /**
     * Excel导出
     */
    @Log("楼盘信息表导出")
    @RequestMapping(value = "/export")
    public void exportExcel(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        loupanService.export(params, response);
    }

}